package com.bs.regsystemapi.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bs.regsystemapi.entity.PatPayRecord;
import com.bs.regsystemapi.entity.PatPerson;
import com.bs.regsystemapi.modal.dto.patpayrecord.*;
import com.bs.regsystemapi.modal.vo.doctor.DoctorInfo;
import com.bs.regsystemapi.modal.vo.patpayrecord.PatPayRecordInfo;
import com.bs.regsystemapi.service.PatPayRecordService;
import com.bs.regsystemapi.service.PatPersonService;
import com.bs.regsystemapi.service.UserService;
import com.bs.regsystemapi.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author ning
 * @Date 2023/1/30 22:08
 */
@RestController
@RequestMapping("/patPayRecord")
@Api(tags = {"缴费记录api接口"})
@Slf4j
public class PatPayRecordController {

    @Autowired
    private PatPayRecordService patPayRecordService;
    @Autowired
    private UserService doctorService;
    @Autowired
    private PatPersonService patPersonService;
    @ApiOperation(value = "获取缴费记录")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseResult getPatPayRecordList(@RequestBody GetPatPayRecordListForm form) {
        ManagePageResult patPayRecord = patPayRecordService.getPatPayRecordList(form);
        return ResponseResult.ok().put(patPayRecord);
    }



    @ApiOperation(value = "通过就诊卡号获取缴费记录")
    @RequestMapping(value = "/getPatPayRecordByPersonNo", method = RequestMethod.GET)
    public ResponseResult getPatPayRecordByPersonNo(@RequestParam("personNo") String personNo) {
        QueryWrapper<PatPayRecord> queryWrapper = new QueryWrapper<>();
        // 条件查询，字段person_no，并且is_delete为1（记录未被删除）
        queryWrapper.eq("person_no", personNo).and(wrapper -> wrapper.eq("is_delete", 1));
        return ResponseResult.ok().put(patPayRecordService.getBaseMapper().selectList(queryWrapper));
    }

    @ApiOperation(value = "获取当前用户下所有状态的缴费记录")
    @RequestMapping(value = "/getPatPayRecordByPatNo", method = RequestMethod.GET)
    public ResponseResult getPatPayRecordByPatNo(@RequestParam(value = "orderState", required = false) String orderState) {
        QueryWrapper<PatPayRecord> queryWrapper = new QueryWrapper<>();
        String patNo;
        try {
            patNo = StpUtil.getLoginIdByToken(ThreadLocalUtils.get("Authorization").toString()).toString();
            log.info("用户号:" + patNo);
        } catch (NullPointerException e) {
            return ResponseResult.error("请求头参数Authorization为空!");
        }
        if (StringUtils.isNotEmpty(orderState)) {
            queryWrapper.eq("order_state", orderState);
        }

        // 条件查询，字段pat_no，并且is_delete为1（记录未被删除）
        queryWrapper.eq("pat_no", patNo)
                    .eq("is_delete", "1");
        // 获取用户下所有的缴费记录
        List<PatPayRecord> patPayRecordList = patPayRecordService.getBaseMapper().selectList(queryWrapper);
        List<PatPayRecordInfo> resultInfo = new ArrayList<>();
        PatPayRecordInfo info;
        for (PatPayRecord payRecord : patPayRecordList) {
            // 构造缴费记录详情对象
            info = new PatPayRecordInfo();
            PatPerson person = patPersonService.getOne(
                    new QueryWrapper<PatPerson>()
                            .eq("person_no", payRecord.getPersonNo())
            );
            BeanUtils.copyProperties(payRecord, info);
            info.setPersonName(person.getPersonName());
            // 添加进结果集
            resultInfo.add(info);
        }
        return ResponseResult.ok().put(resultInfo);

    }

    @ApiOperation(value = "通过医生no获取缴费记录")
    @RequestMapping(value = "/getPatPayRecordByDoctorNo", method = RequestMethod.GET)
    public ResponseResult getPatPayRecordByDoctorNo(@RequestParam("doctorNo") String doctorNo) {
        QueryWrapper<PatPayRecord> queryWrapper = new QueryWrapper<>();
        // 条件查询，字段doctor_no，并且is_delete为1（记录未被删除）
        queryWrapper.eq("doctor_no", doctorNo).and(wrapper -> wrapper.eq("is_delete", 1));
        return ResponseResult.ok().put(patPayRecordService.getBaseMapper().selectList(queryWrapper));
    }

    @ApiOperation(value = "通过订单号获取缴费记录")
    @RequestMapping(value = "/getPatPayRecordByOrderNo", method = RequestMethod.GET)
    public ResponseResult getPatPayRecordByOrderNo(@RequestParam("orderNo") String orderNo) {
        QueryWrapper<PatPayRecord> queryWrapper = new QueryWrapper<>();
        // 条件查询，字段order_no，并且is_delete为1（记录未被删除）
        queryWrapper.eq("order_no", orderNo).and(wrapper -> wrapper.eq("is_delete", 1));
        return ResponseResult.ok().put(patPayRecordService.getOne(queryWrapper));
    }

    @ApiOperation(value = "完成缴费")
    @RequestMapping(value = "/updatePatPayRecord", method = RequestMethod.POST)
    public ResponseResult updatePatPayRecord(@RequestBody @Validated FinishPay finishPay) {
        String patNo = "";
        try {
            // 获取用户No(patNo)
            patNo = StpUtil.getLoginIdByToken(ThreadLocalUtils.get("Authorization").toString()).toString();
            log.info("用户号:" + patNo);
        } catch (NullPointerException e) {
            return ResponseResult.error("请求头参数Authorization为空!");
        }

        Date payTime = new Date();
        // 获取就诊人信息
        PatPerson patPerson = patPersonService.getOne(new QueryWrapper<PatPerson>().eq("pat_no", patNo));
        // 通过订单号获取指定订单
        QueryWrapper<PatPayRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",finishPay.getOrderNo());
        PatPayRecord payRecord = patPayRecordService.getBaseMapper().selectOne(queryWrapper);
        if (payRecord != null) {
            // 缴费时间
            payRecord.setPayTime(payTime);
            // 订单状态
            payRecord.setOrderState("1");
            // 付款类型
            payRecord.setPayType(finishPay.getPayType());
            // 支付方式
            payRecord.setPayMode(finishPay.getPayMode());
            // 订单完成时间
            payRecord.setFinishTime(new Date());
            // 就诊卡号
            payRecord.setPersonNo(patPerson.getPersonNo());
            patPayRecordService.getBaseMapper().updateById(payRecord);
            return ResponseResult.ok("缴费成功");
        }
        return ResponseResult.error("订单号不存在，请传入正确的订单号!");
    }


    @ApiOperation(value = "新增缴费")
    @RequestMapping(value = "/savePatPayRecord", method = RequestMethod.POST)
    public ResponseResult savePatPayRecord(@RequestBody @Validated AddPayRecord patPayRecord) {
        String patNo = "";
        try {
            // 获取用户No(patNo)
            patNo = StpUtil.getLoginIdByToken(ThreadLocalUtils.get("Authorization").toString()).toString();
            log.info("用户号:" + patNo);
        } catch (NullPointerException e) {
            return ResponseResult.error("请求头参数Authorization为空!");
        }

        PatPerson patPerson = patPersonService.getOne(
                new QueryWrapper<PatPerson>()
                        .eq("pat_no", patNo)
        );

        PatPayRecord payRecord = new PatPayRecord();
        // 生成订单号(8位随机数+10位时间戳)
        String time = Long.toString(System.currentTimeMillis());
        time = time.substring(0, time.length()-3);
        String orderNo = RandomUtils.getrandom(8) + time;
        payRecord.setOrderNo(orderNo);
        // 订单创建时间
        payRecord.setCreatTime(new Date());
        // 是否删除：1 正常
        payRecord.setIsDelete("1");
        // 用户No
        payRecord.setPatNo(patNo);
        // 预约号
        payRecord.setRegNo(patPayRecord.getRegNo());
        // 医生No
        payRecord.setDoctorNo(patPayRecord.getDoctorNo());
        // 就诊卡号
        payRecord.setPersonNo(patPerson.getPersonNo());

        DoctorInfo doctorInfo = doctorService.getDoctorInfo(patPayRecord.getDoctorNo());
        // 挂号费用
        payRecord.setUserCost(Double.parseDouble(doctorInfo.getReservePrice()));

        patPayRecordService.save(payRecord);
        return ResponseResult.ok();
    }


    @ApiOperation(value = "缴费详情")
    @RequestMapping(value = "/patPayRecordDetail", method = RequestMethod.GET)
    public ResponseResult patPayRecordDetail(@RequestBody @Validated PatPayRecordDetail detail) {
        // 通过订单编号查询订单记录
        QueryWrapper<PatPayRecord> queryWrapper = new QueryWrapper<>();
        // 条件查询，字段order_no，并且is_delete为1（记录未被删除）
        queryWrapper.eq("order_no", detail.getOrderNo());
        queryWrapper.eq("is_delete", "1");
        // 缴费记录部分详情
        PatPayRecord patPayRecord = patPayRecordService.getOne(queryWrapper);
        // 医生详情部分
        DoctorInfo doctorInfo = doctorService.getDoctorInfo(detail.getDoctorNo());
        Map<String, Object> result = new HashMap<>(10);
        result.put("patPayRecord", patPayRecord);
        result.put("doctorInfo", doctorInfo);
        return ResponseResult.ok().put(result);
    }

    @ApiOperation(value = "获取预约订单列表")
    @PostMapping(value = "/orderList")
    public ResponseResult getOrderList(@RequestBody QueryOrderListForm form) {
        ManagePageResult orderList = patPayRecordService.getOrderList(form);
        return ResponseResult.ok().put(orderList);
    }

    @ApiOperation(value = "删除订单")
    @PostMapping(value = "/delete")
    public ResponseResult deleteOrder(@RequestBody String orderNo) {
        if(StringUtils.isEmpty(orderNo)) {
            return ResponseResult.error("订单no不能为空");
        }
        patPayRecordService.deleteOrder(orderNo);
        return ResponseResult.ok();
    }
}
